import { redisClientOpt } from "./redisOperate";

/**
 * 存储验证码
 * @param phone 手机号
 * @param code 验证码
 * @param pexpire 过期时间 单位毫秒
 * @returns 
 */
export const storeCode = (phone: string, code: string, pexpire: number = 60 * 1000) => {
  return new Promise((resolve, reject) => {
    redisClientOpt('set', `code:${phone}`, code, pexpire, (err, result) => {
      if (err) {
        reject(err);
      } else {
        resolve(true);
      }
    });
  });
}

/**
 * 获取验证码
 * @param phone 手机号
 * @returns 
 */
export const getCode = (phone: string) => {
  return new Promise((resolve, reject) => {
    redisClientOpt('get', `code:${phone}`, null, 0, (err, result) => {
      if (err) {
        reject(err);
      } else {
        resolve(result);
      }
    });
  });
}
